#ifndef STAR
#define STAR

#include <SDL.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef LOADTEXTURE
#include "loadtexture.h"
#endif
#include <iostream>
using namespace std;

#include <time.h>


#define SIZE_STAR_VERTICES_ARRAY 12

/**
 * Encapsulates one Star
 * Don't forget to call deleteStarTexture()
 * when done with the texture data!
 */
class Star
{
private:
	static GLfloat Star_Vertices[SIZE_STAR_VERTICES_ARRAY];
	static GLuint Star_Texture_Handle[1];
	static bool    texture_Loaded;

	GLbyte _red, _green, _blue;


public:
	static int loadStarTexture();
	static int deleteStarTexture();


	Star();
	void draw();
};

GLuint Star::Star_Texture_Handle[] = { 0 };
GLfloat Star::Star_Vertices[] =
{
			1.0f, 0.0f,
			1.0f, 1.0f,
			0.0f, 1.0,

			1.0f, 0.0f,
			0.0f, 1.0f,
			0.0f, 0.0f
};
bool Star::texture_Loaded = false;
/**
 * Static method to load the texture for Star
 * This method is static because we don't want to
 * load the image for every single Star object we create;
 * they're all drawing the same one anyway.
 * This method returns true on either successfully loading the texture
 * OR if the texture was already successfully loaded.
 * Returns false on failure.
 */
int Star::loadStarTexture()
{
	if(texture_Loaded == true)
	{
		cout << "loadStarTexture called for no result\n";
		return true;
	}

	Star_Texture_Handle[0] = loadTexturewithSDL("star.png", GL_RGBA);
	if(!Star_Texture_Handle[0])
	{
		cout << "FAILED TO LOAD Data/star.bmp!\n";
		return false;
	}
	else
	{
		cout << "Loaded Data/star.bmp successfully.\n";
		texture_Loaded = true;
		return true;
	}

}
/**
 * If a texture was loaded, delete it
 */
int Star::deleteStarTexture()
{
	if(texture_Loaded)
		glDeleteTextures(1, Star_Texture_Handle);
}

/**
 * CTOR
 * Calls loadStarTexture
 */
Star::Star()
{
	if(!loadStarTexture)
	{
		cout << "ERROR LOADING STAR from Star() CTOR\n";
	}


	_red = rand() % 256 + 50;
	_green=rand() % 256 + 50;
	_blue =rand() % 256 + 50;
}

GLshort coordData[] =
{
		1, 0,
		1, 1,
		0, 1,

		1, 0,
		0, 1,
		0, 0
};

GLfloat star_normal_data[] =
{
	0, 0, 1,
	0, 0, 1,
	0, 0, 1,
	
	0, 0, 1,
	0, 0, 1,
	0, 0, 1
	
};

void Star::draw()
{
	glBindTexture(GL_TEXTURE_2D, Star_Texture_Handle[0]);

	//set the randomized color
	glColor4b(_red, _green, _blue, 120);
	
	
	//point OpenGL to our array
	glNormalPointer(GL_FLOAT, 0, star_normal_data);
	glTexCoordPointer(2, GL_SHORT, 0, coordData);

	glVertexPointer(2, GL_FLOAT, 0, Star_Vertices);



	glDrawArrays(GL_TRIANGLES, 0, SIZE_STAR_VERTICES_ARRAY);
}


#endif
